<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               :title="!dataForm.id ? $t('add') : $t('update')"
               :modal="false"
               class="user-dialog add-or-updata"
               width="40%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="dataForm"
               v-loading="dataListLoading"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               :rules="rules"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="120px">
        <!-- <div> -->
        <el-form-item prop='ruleName'
                      label="规则名称">
          <el-input v-model.trim='dataForm.ruleName'
                    class='form-item'
                    maxlength="20"
                    show-word-limit
                    clearable
                    placeholder='请输入规则名称'></el-input>
        </el-form-item>
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item prop="authorizeType"
                          label="授权时长">
              <el-select v-model="dataForm.authorizeType"
                         placeholder="时间类型"
                         class="width-100">
                <el-option v-for="item in timeList"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id"
                           @click.native="showDateInput = item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="authorize"
                          label-width="0">
              <el-input v-show="showDateInput != '3'"
                        type="number"
                        placeholder="请输入时长"
                        v-model="dataForm.authorize">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item prop="startTime"
                          label="每日停车时段">
              <el-time-picker v-model="dataForm.startTime"
                              style="width:100%"
                              value-format="HH:mm:ss"
                              placeholder="选择日期">
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="finishTime"
                          label="至"
                          label-width="40px">
              <el-time-picker v-model="dataForm.finishTime"
                              style="width:100%"
                              value-format="HH:mm:ss"
                              placeholder="选择日期">
              </el-time-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item prop="ruleDescription"
                      label="规则描述">
          <el-input v-model.trim="dataForm.ruleDescription"
                    type="textarea"
                    placeholder="请输入描述"
                    :autosize="{ minRows: 2, maxRows: 4}"
                    maxlength="200"
                    show-word-limit></el-input>
        </el-form-item>
        <el-form-item prop="status"
                      label="状态">
          <el-radio-group v-model="dataForm.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- </div> -->
      </el-form>
      <template slot="footer">
        <el-button @click="closeDialog()">{{ $t("cancel") }}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule, minFormValidate],
  data () {
    return {
      mixinViewModuleOptions: {
        deleteImgURL: '',
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addURL: 'tb/authorizationRules/add', // 添加或修改接口，API地址
        updateURL: 'tb/authorizationRules/revise', // 添加或修改接口，API地址
        getDataInfoURL: 'tb/authorizationRules/describe?id=', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      visible: false,
      showDateInput: '0',
      dataForm: {
        status: '0'
      },
      rules: {
        ruleName: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
        // authorize: [{ required: false, message: '请输入时长', trigger: 'blur' }],
        authorizeType: [{ required: true, message: '请选择授权类型', trigger: 'change' }],
        startTime: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
        finishTime: [{ required: true, message: '请选择结束时间', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      },
      timeList: [{
        id: '0',
        name: '周'
      }, {
        id: '1',
        name: '月'
      }, {
        id: '2',
        name: '年'
      }, {
        id: '3',
        name: '长期'
      } ]
    }
  },
  updated () {
    let that = this
    this.$nextTick(() => {
      that.showDateInput = that.dataForm.authorizeType
    })
  },
  methods: {
    init (data) {
      if (data) {
        this.dataForm = data
      }
      this.visible = true
      this.$nextTick(() => {
      })
    },
    closeDialog () {
    //   this.$refs.dataForm.resetFields()
      this.dataForm.ruleName = ''
      this.dataForm.authorizeType = ''
      this.dataForm.authorize = ''
      this.dataForm.startTime = ''
      this.dataForm.finishTime = ''
      this.dataForm.ruleDescription = ''
      this.dataForm.status = '0'
      this.showDateInput = '0'
      this.$emit('callBack')
      this.visible = false
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            const url = this.dataForm.id ? this.mixinViewModuleOptions.updateURL
              : this.mixinViewModuleOptions.addURL
            // this.dataForm.authorize = Number(this.dataForm.authorize)
            if (!this.dataForm.id) {
              this.$http.post(url, this.dataForm).then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg)
                }
                this.$message.success('新增成功')
                this.$emit('callBack')
                this.closeDialog()
              })
            } else {
              this.$http.put(url, this.dataForm).then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg)
                }
                this.$message.success('保存成功')
                this.$emit('callBack')
                this.closeDialog()
              })
            }
          }
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>

<style lang="scss" scoped>
</style>
